<template>
  <div class="ResourceList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          ref="form"
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
        >
          <el-form-item prop="name" label="资源名称">
            <el-input
              v-model="formInline.name"
              placeholder="资源名称"
            ></el-input>
          </el-form-item>
          <el-form-item prop="url" label="资源路径">
            <el-input
              v-model="formInline.url"
              placeholder="资源路径"
            ></el-input>
          </el-form-item>
          <el-form-item prop="categoryId" label="资源分类">
            <el-select
              v-model="formInline.categoryId"
              placeholder="资源分类"
              clearable
            >
              <el-option
                v-for="ele in resourceCategories"
                :key="ele.id"
                :label="ele.name"
                :value="ele.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :disabled="isLoading" type="primary" @click="onSubmit"
              >查询</el-button
            >
            <el-button :disabled="isLoading" @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="isLoading" :data="resources" style="width: 100%">
        <el-table-column type="index" label="编号" min-width="150">
        </el-table-column>
        <el-table-column prop="name" label="资源名称" min-width="180">
        </el-table-column>
        <el-table-column prop="url" label="资源路径" min-width="180">
        </el-table-column>
        <el-table-column prop="description" label="描述" min-width="180">
        </el-table-column>
        <el-table-column prop="createdTime" label="添加时间" min-width="180">
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div
        style="margin-top:15px;display:flex;justify-content:center;align-items:center;"
      >
        <el-pagination
          :disabled="isLoading"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formInline.current"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="formInline.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourcePages, getAllResources } from '../api/index'
interface Row {
  id: string;
}
export default Vue.extend({
  name: 'ResourceList',
  data () {
    return {
      resources: [],
      formInline: {
        name: '',
        url: '',
        current: 1,
        size: 5,
        categoryId: ''
      },
      total: 0,
      resourceCategories: [],
      isLoading: true
    }
  },
  async created () {
    await this.getAllResources()
  },
  methods: {
    async getAllResources () {
      const { data } = await getAllResources()
      console.log(data)
      this.resourceCategories = data.data
    },
    onSubmit () {
      console.log('submit!')
      if (this.formInline.current !== 1) {
        this.formInline.current = 1
      } else {
        this.getResourcePages()
      }
    },
    async onReset () {
      this.$refs.form.resetFields() // 主要清除校验
      if (this.formInline.current !== 1) {
        this.formInline = {
          name: '',
          url: '',
          current: 1,
          size: 5,
          categoryId: ''
        }
      } else {
        this.formInline = {
          name: '',
          url: '',
          current: 1,
          size: 5,
          categoryId: ''
        }
        this.getResourcePages()
      }
    },
    async getResourcePages () {
      this.isLoading = true
      const { data } = await getResourcePages(this.formInline)
      console.log(data)
      this.resources = data.data.records.map(ele => {
        ele.createdTime = this.formatDate(ele.createdTime)
        return ele
      })
      this.total = data.data.total
      this.isLoading = false
    },
    handleEdit (index: number, row: Row) {
      console.log(index, row)
    },
    async handleDelete (index: number, row: Row) {
      console.log(index, row)
    },
    handleSizeChange (val: number) {
      console.log(`每页 ${val} 条`)
      this.formInline.size = val
      if (this.formInline.current !== 1) {
        this.formInline.current = 1
      } else {
        this.getResourcePages()
      }
    },
    handleCurrentChange (val: number) {
      console.log(`当前页: ${val}`)
      this.formInline.current = val
    }
  },
  watch: {
    'formInline.current': {
      immediate: true,
      handler (n: number, o: number) {
        console.log('pagechange', n, o)
        this.getResourcePages()
      }
    }
  }
})
</script>

<style lang="scss" scoped></style>
